﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace MeTalker.Aide.Service
{
    delegate void HttpCallBack(object sender, byte[] buffer, int size);
    internal class HttpClient
    {
        static readonly int REQ_TIME_OUT = 15000;
        internal byte[] Request(string url, byte[] buffer)
        {
            return Request(url, buffer, buffer != null ? buffer.Length : 0);
        }

        internal static byte[] RequestHeart(string url, byte[] buffer, int size)
        {
            try
            {
                HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
                request.Proxy = null;
                request.Method = "POST";
                request.KeepAlive = false;
                request.ContentLength = size;
                request.ContentType = "application/x-www-form-urlencoded";  
                request.Timeout = REQ_TIME_OUT;
                Stream stream = request.GetRequestStream();
                stream.Write(buffer, 0, size);
                stream.Flush();
                stream.Close();
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                System.IO.Stream responseStream = response.GetResponseStream();
                try
                {
                    List<byte> bytes = new List<byte>();
                    int b;
                    while ((b = responseStream.ReadByte()) != -1)
                    {
                        bytes.Add((byte)b);
                    }
                    return bytes.ToArray();
                }
                finally
                {
                    if (responseStream != null)
                        responseStream.Close();
                }
            }
            catch (Exception ex)
            {
                if (UtilHelper.DEBUG)
                    MuchInfo.Logs.MILog.Error("RequestHeart", ex);
                return null;
            }
        }

        internal byte[] Request(string url , byte[] buffer, int size)
        {
            try
            {
                if (size <= 0)
                    return null;
                if (UtilHelper.DEBUG)
                    MuchInfo.Logs.MILog.Info("请求登录!");
                HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
                request.Method = "POST";
                request.Proxy = null;
                request.ContentLength = size;
                request.ContentType = "application/x-www-form-urlencoded";  
                request.KeepAlive = false;
                request.Timeout = REQ_TIME_OUT;
                Stream stream = request.GetRequestStream();
                stream.Write(buffer, 0, size);
                stream.Flush();
                stream.Close();
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                Stream responseStream = response.GetResponseStream();
                try
                {
                    List<byte> bytes = new List<byte>();
                    int b;
                    while ((b = responseStream.ReadByte()) != -1)
                    {
                        bytes.Add((byte)b);
                    }
                    return bytes.ToArray();
                }
                finally
                {
                    if (responseStream != null)
                        responseStream.Close();
                }
                //System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
                //return reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                if (UtilHelper.DEBUG)
                    MuchInfo.Logs.MILog.Error("RequestHeart", ex);
                return null;
            }
        }
    }
}